import { JwtService } from '@nestjs/jwt';
import { Injectable } from '@nestjs/common';

import { JwtPayload } from './interfaces/jwt-payload.interface';
import { UserService } from '../user/user.service';

@Injectable()
export class AuthService {
  constructor(
    private readonly userService: UserService,
    private readonly jwtService: JwtService
  ) {}

  // 签发token
  async signIn(user: JwtPayload): Promise<string> {
    return this.jwtService.sign(user);
  }

  // 验证token
  async validateUser(payload: JwtPayload): Promise<any> {
    return await this.userService.findUserByUserName(payload.username);
  }
}
